<template>
  <div class="login-container">
    <el-form
      ref="loginFormRef"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >
      <div class="title-container pr">
        <h3 class="title">用户登录</h3>
      </div>
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon="user" />
        </span>
        <el-input
          v-model="loginForm.username"
          placeholder="请输入用户名"
          type="text"
        />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon="password" />
        </span>
        <span class="show-pwd">
          <svg-icon
            :icon="passwordType === 'text' ? 'eye-open' : 'eye'"
            @click="onChangePwdType"
          />
        </span>
        <el-input
          v-model="loginForm.password"
          placeholder="请输入登录密码"
          :type="passwordType"
        />
      </el-form-item>
      <el-button
        class="login-btn"
        size="large"
        type="primary"
        :loading="loading"
        @click="handleLogin"
        >登录</el-button
      >
    </el-form>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { validatePassword } from "@/utils/validate";
import { useStore } from "vuex";
const loginForm = ref({
  username: "admin",
  password: "",
});
const loginRules = ref({
  username: [
    {
      required: true,
      trigger: "blur",
      message: "请输入用户名",
    },
  ],
  password: [
    {
      required: true,
      trigger: "blur",
      validator: validatePassword(),
    },
  ],
});
/**
 * 切换password类型
 */
const passwordType = ref("password");
const onChangePwdType = () => {
  if (passwordType.value === "password") {
    passwordType.value = "text";
  } else {
    passwordType.value = "password";
  }
};

// 登录动作
const loading = ref(false);
const store = useStore();
const loginFormRef = ref(null);
const handleLogin = () => {
  loginFormRef.validate((valid) => {
    if (!valid) return;
    loading.value = false;
    store
      .dispatch("user/Login", loginForm.value)
      .then(() => {
        loading.value = false;
        // TODO: 进行登录后处理
      })
      .catch((err) => {
        console.log(err);
        loading.value = false;
      });
  });
};
</script>

<style lang="scss" scoped>
$bg: #2d3a4b;
$drak_gray: #889aa4;
$light_gray: #eee;
$cursor: #fff;

.login-container {
  min-height: 100%;
  min-width: 100%;
  background-color: $bg;
  overflow: hidden;
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
    ::v-deep(.el-form-item) {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background-color: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
      margin-bottom: 25px;
      .el-form-item__error {
        padding-top: 6px;
      }
    }
    ::v-deep(.el-input) {
      display: inline-block;
      height: 47px;
      width: 85%;
      .el-input__wrapper {
        width: 100%;
        height: 100%;
        background-color: transparent;
        border: none;
        box-shadow: none;
      }
      input {
        background-color: transparent;
        border: none;
        border-radius: 0px;
        color: $light_gray;
        caret-color: $cursor;
      }
    }

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0 auto 40px;
      text-align: center;
      font-weight: bold;
    }
    .svg-container {
      padding: 0px 10px 0 15px;
      color: $drak_gray;
    }
    .show-pwd {
      position: absolute;
      cursor: pointer;
      user-select: none;
      color: $drak_gray;
      right: 10px;
    }
  }
}
.login-btn {
  width: 100%;
  margin-top: 20px;
}
</style>
